package com.facebook.drawee.drawable;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import com.facebook.common.internal.Preconditions;

import javax.annotation.Nullable;

public class ArrayDrawable extends Drawable implements Drawable.Callback, TransformAwareDrawable, TransformCallback {
    public final Drawable[] mLayers;
    public final DrawableParent[] mDrawableParents;
    private TransformCallback mTransformCallback;
    private final DrawableProperties mDrawableProperties;
    private final Rect mTmpRect;
    private boolean mIsStateful;
    private boolean mIsStatefulCalculated;
    private boolean mIsMutated;

    public ArrayDrawable(Drawable[] layers) {
        int v0 = 0;
        this.mDrawableProperties = new DrawableProperties();
        this.mTmpRect = new Rect();
        this.mIsStateful = false;
        this.mIsStatefulCalculated = false;
        this.mIsMutated = false;
        Preconditions.checkNotNull(layers);
        this.mLayers = layers;
        while (v0 < this.mLayers.length) {
            DrawableUtils.setCallbacks(this.mLayers[v0], ((Drawable.Callback) this), ((TransformCallback) this));
            ++v0;
        }
        this.mDrawableParents = new DrawableParent[this.mLayers.length];
    }

    @Nullable
    public final Drawable getDrawable(int arg4) {
        boolean v1 = true;
        Preconditions.checkArgument(arg4 >= 0);
        if (arg4 >= this.mLayers.length) {
            v1 = false;
        }
        Preconditions.checkArgument(v1);
        return this.mLayers[arg4];
    }

    @Nullable
    public final Drawable setDrawable(int arg5, @Nullable Drawable drawable) {
        boolean v1 = true;
        Drawable.Callback v3 = null;
        boolean v0 = arg5 >= 0 ? true : false;
        Preconditions.checkArgument(v0);
        if (arg5 >= this.mLayers.length) {
            v1 = false;
        }

        Preconditions.checkArgument(v1);
        Drawable v0_1 = this.mLayers[arg5];
        if (drawable != v0_1) {
            if (drawable != null && (this.mIsMutated)) {
                drawable.mutate();
            }

            DrawableUtils.setCallbacks(this.mLayers[arg5], v3, ((TransformCallback) v3));
            DrawableUtils.setCallbacks(drawable, v3, ((TransformCallback) v3));
            DrawableUtils.setDrawableProperties(drawable, this.mDrawableProperties);
            if (drawable != null) {
                drawable.setBounds(this.getBounds());
                drawable.setLevel(this.getLevel());
                drawable.setState(this.getState());
                drawable.setVisible(this.isVisible(), false);
            }

            DrawableUtils.setCallbacks(drawable, ((Drawable.Callback) this), ((TransformCallback) this));
            this.mIsStatefulCalculated = false;
            this.mLayers[arg5] = drawable;
            this.invalidateSelf();
        }
        return v0_1;
    }

    public final void getTransform(Matrix matrix) {
        if (this.mTransformCallback != null) {
            this.mTransformCallback.getTransform(matrix);
        } else {
            matrix.reset();
        }
    }

    public final void getRootBounds(RectF rectF) {
        if (this.mTransformCallback != null) {
            this.mTransformCallback.getRootBounds(rectF);
        } else {
            rectF.set(this.getBounds());
        }
    }

    public final void setTransformCallback(TransformCallback transformCallback) {
        this.mTransformCallback = transformCallback;
    }

    public void draw(Canvas canvas) {
        for (int i = 0; i < this.mLayers.length; ++i) {
            Drawable layer = this.mLayers[i];
            if (layer != null) {
                layer.draw(canvas);
            }
        }
    }

    public int getIntrinsicHeight() {
        int v2 = -1;
        int v0 = 0;
        int v1 = v2;
        while (v0 < this.mLayers.length) {
            Drawable v3 = this.mLayers[v0];
            if (v3 != null) {
                v1 = Math.max(v1, v3.getIntrinsicHeight());
            }

            ++v0;
        }

        if (v1 <= 0) {
            v1 = v2;
        }

        return v1;
    }

    public int getIntrinsicWidth() {
        int v2 = -1;
        int v0 = 0;
        int v1 = v2;
        while (v0 < this.mLayers.length) {
            Drawable v3 = this.mLayers[v0];
            if (v3 != null) {
                v1 = Math.max(v1, v3.getIntrinsicWidth());
            }

            ++v0;
        }

        if (v1 <= 0) {
            v1 = v2;
        }

        return v1;
    }

    public int getOpacity() {
        int v1;
        if (this.mLayers.length == 0) {
            v1 = -2;
        } else {
            v1 = -1;
            int v0;
            for (v0 = 1; v0 < this.mLayers.length; ++v0) {
                Drawable drawable = this.mLayers[v0];
                if (drawable != null) {
                    v1 = Drawable.resolveOpacity(v1, drawable.getOpacity());
                }
            }
        }

        return v1;
    }

    public boolean getPadding(Rect padding) {
        int v0 = 0;
        padding.left = 0;
        padding.top = 0;
        padding.right = 0;
        padding.bottom = 0;
        Rect rect = this.mTmpRect;
        while (v0 < this.mLayers.length) {
            Drawable drawable = this.mLayers[v0];
            if (drawable != null) {
                drawable.getPadding(rect);
                padding.left = Math.max(padding.left, rect.left);
                padding.top = Math.max(padding.top, rect.top);
                padding.right = Math.max(padding.right, rect.right);
                padding.bottom = Math.max(padding.bottom, rect.bottom);
            }

            ++v0;
        }

        return true;
    }

    public void invalidateDrawable(Drawable arg1) {
        this.invalidateSelf();
    }

    public boolean isStateful() {
        if (!this.mIsStatefulCalculated) {
            this.mIsStateful = false;
            int v0;
            for (v0 = 0; v0 < this.mLayers.length; ++v0) {
                Drawable v2 = this.mLayers[v0];
                boolean v4 = this.mIsStateful;
                int v2_1 = v2 == null || !v2.isStateful() ? 0 : 1;
                this.mIsStateful = v2_1 > 0 | v4;
            }

            this.mIsStatefulCalculated = true;
        }

        return this.mIsStateful;
    }

    public Drawable mutate() {
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v1 = this.mLayers[v0];
            if (v1 != null) {
                v1.mutate();
            }
        }

        this.mIsMutated = true;
        return this;
    }

    protected void onBoundsChange(Rect arg3) {
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v1 = this.mLayers[v0];
            if (v1 != null) {
                v1.setBounds(arg3);
            }
        }
    }

    protected boolean onLevelChange(int arg4) {
        int v0 = 0;
        boolean v1 = false;
        while (v0 < this.mLayers.length) {
            Drawable v2 = this.mLayers[v0];
            if (v2 != null && (v2.setLevel(arg4))) {
                v1 = true;
            }

            ++v0;
        }

        return v1;
    }

    protected boolean onStateChange(int[] arg4) {
        int v0 = 0;
        boolean v1 = false;
        while (v0 < this.mLayers.length) {
            Drawable v2 = this.mLayers[v0];
            if (v2 != null && (v2.setState(arg4))) {
                v1 = true;
            }

            ++v0;
        }

        return v1;
    }

    public void scheduleDrawable(Drawable arg2, Runnable arg3, long arg4) {
        this.scheduleSelf(arg3, arg4);
    }

    public void setAlpha(int arg3) {
        this.mDrawableProperties.alpha = arg3;
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v1 = this.mLayers[v0];
            if (v1 != null) {
                v1.setAlpha(arg3);
            }
        }
    }

    public void setColorFilter(ColorFilter arg3) {
        this.mDrawableProperties.setColorFilter(arg3);
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v1 = this.mLayers[v0];
            if (v1 != null) {
                v1.setColorFilter(arg3);
            }
        }
    }

    public void setDither(boolean arg3) {
        this.mDrawableProperties.setDither(arg3);
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v1 = this.mLayers[v0];
            if (v1 != null) {
                v1.setDither(arg3);
            }
        }
    }

    public void setFilterBitmap(boolean arg3) {
        this.mDrawableProperties.setFilterBitmap(arg3);
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v1 = this.mLayers[v0];
            if (v1 != null) {
                v1.setFilterBitmap(arg3);
            }
        }
    }

    public boolean setVisible(boolean arg4, boolean arg5) {
        boolean v1 = super.setVisible(arg4, arg5);
        int v0;
        for (v0 = 0; v0 < this.mLayers.length; ++v0) {
            Drawable v2 = this.mLayers[v0];
            if (v2 != null) {
                v2.setVisible(arg4, arg5);
            }
        }

        return v1;
    }

    public void unscheduleDrawable(Drawable arg1, Runnable arg2) {
        this.unscheduleSelf(arg2);
    }
}

